package com.jeeplus.test.education.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jeeplus.test.education.domain.vo.EducationInfoVo;
import com.jeeplus.test.education.service.EducationService;
import com.jeeplus.test.tools.resp.R;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.*;
import java.util.stream.Collectors;

/**
 * 继续教育学院管理控制器
 *
 * @author 马贵东
 * @since 2025-03-05
 */
@RestController
@RequestMapping("/educationInfo")
public class EducationInfoController {

    @Autowired
    private EducationService educationService;


//    @ApiOperation("分页查询各部门提交的录取信息")
//    @GetMapping("/completePage")
//    public ResponseEntity<Page<Map<String, Object>>> getCompletePage(PostgraduateReqVo admissionInfoReqVo,
//                                                                          @RequestParam(defaultValue = "1") int pageNum,
//                                                                          @RequestParam(defaultValue = "10") int pageSize) {
//        Page<PostgraduateReqVo> page = new Page<>(pageNum, pageSize);
//        Page<Map<String, Object>> pageResult = admissionInfoService.getAdmissionInfoPage(admissionInfoReqVo, page);
//
//        // 确保 null 值字段被填充
//        List<Map<String, Object>> modifiedRecords = pageResult.getRecords().stream()
//                .map(record -> {
//                    Map<String, Object> modifiedRecord = new HashMap<>();
//                    record.forEach((key, value) -> modifiedRecord.put(key, value != null ? value : ""));
//                    return modifiedRecord;
//                })
//                .collect(Collectors.toList());
//
//        Page<Map<String, Object>> modifiedPage = new Page<>(pageResult.getCurrent(), pageResult.getSize(), pageResult.getTotal());
//        modifiedPage.setRecords(modifiedRecords);
//
//        return ResponseEntity.ok()
//                .contentType(MediaType.APPLICATION_JSON)
//                .body(modifiedPage);
//    }

    @ApiOperation("继续教育学院分页查询")
    @GetMapping("/page")
    public ResponseEntity<Page<Map<String, Object>>> getEducationInfoPage(
                                                                          EducationInfoVo educationInfoVo,
                                                                          @RequestParam(defaultValue = "1") int pageNum,
                                                                          @RequestParam(defaultValue = "10") int pageSize) {


        Page<EducationInfoVo> page = new Page<>(pageNum, pageSize);
        Page<Map<String, Object>> pageResult = educationService.getEducationInfoPage(educationInfoVo, page);

        // 确保 null 值字段被填充
        List<Map<String, Object>> modifiedRecords = pageResult.getRecords().stream()
                .map(record -> {
                    Map<String, Object> modifiedRecord = new HashMap<>();
                    record.forEach((key, value) -> modifiedRecord.put(key, value != null ? value : ""));
                    return modifiedRecord;
                })
                .collect(Collectors.toList());

        Page<Map<String, Object>> modifiedPage = new Page<>(pageResult.getCurrent(), pageResult.getSize(), pageResult.getTotal());
        modifiedPage.setRecords(modifiedRecords);

        return ResponseEntity.ok()
                .contentType(MediaType.APPLICATION_JSON)
                .body(modifiedPage);
    }


    @ApiOperation("继续教育修改接口")
    @PostMapping("/update")
    public R update(@RequestBody EducationInfoVo educationInfoVo){

        educationService.update(educationInfoVo);
        return R.suc();
    }

}
